package com.haoxi.gateway.test.message;

import com.haoxi.gateway.test.SGIPConstant;
import com.haoxi.gateway.test.util.TypeConvert;

public class SGIPBindRepMessage extends SGIPMessage {

    public SGIPBindRepMessage(byte[] seqid, int result)
            throws IllegalArgumentException {
        if (result < 0 || result > 255) {
            throw new IllegalArgumentException(String.valueOf(String
                    .valueOf((new StringBuffer(String.valueOf(String
                            .valueOf(SGIPConstant.DELIVER_REPINPUT_ERROR))))
                            .append(":result").append(
                                    SGIPConstant.INT_SCOPE_ERROR))));
        } else {
            int len = 29;
            buf = new byte[len];
            TypeConvert.int2byte(len, buf, 0);
            TypeConvert.int2byte(SGIPConstant.Bind_Rep_Command_Id, buf, 4);
            System.arraycopy(seqid, 0, buf, 8, 12);
            buf[20] = (byte) result;
        }
    }

    public SGIPBindRepMessage(byte buf[]) throws IllegalArgumentException {
        super.buf = new byte[29];
        if (buf.length != 29) {
            throw new IllegalArgumentException(SGIPConstant.SMC_MESSAGE_ERROR);
        } else {
            System.arraycopy(buf, 0, super.buf, 0, buf.length);
            src_node_Id = TypeConvert.byte2int(super.buf, 8);
            time_Stamp = TypeConvert.byte2int(super.buf, 12);
            sequence_Id = TypeConvert.byte2int(super.buf, 16);
        }
    }

    public int getMsgLength() {
        message_Length = TypeConvert.byte2int(super.buf, 0);
        return message_Length;

    }

    public int getTime_Stamp() {
        time_Stamp = TypeConvert.byte2int(super.buf, 12);
        return time_Stamp;
    }

    public int getSequence_Id() {
        sequence_Id = TypeConvert.byte2int(super.buf, 16);
        return sequence_Id;
    }

    public int getSrcNodeId() {
        src_node_Id = TypeConvert.byte2int(super.buf, 8);
        return src_node_Id;
    }

    public int getResult() {
        return buf[20];
    }

    public String toString() {
        String tmpStr = "SGIP_BIND_REP: ";
        StringBuffer buff = new StringBuffer(tmpStr);
        buff.append("\r\nmessage_Length=").append(this.getMsgLength()).append(
                "\r\n").append("CommandId=").append(this.getCommandId())
                .append("\r\n").append("src_node_Id=").append(
                        this.getSrcNodeId()).append("\r\n").append(
                        "time_Stamp=").append(this.getTime_Stamp()).append(
                        "\r\n").append("sequence_Id=").append(
                        this.getSequence_Id()).append("\r\n").append("result=")
                .append(getResult());
        return buff.toString();
    }

    public int getCommandId() {
        command_ID = TypeConvert.byte2int(super.buf, 4);
        return command_ID;
    }
}
